package online.longzipeng.cinemasys.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import online.longzipeng.cinemasys.common.annotion.NeedPermissions;
import online.longzipeng.cinemasys.common.exception.AppException;
import online.longzipeng.cinemasys.common.result.ResponseEnum;
import online.longzipeng.cinemasys.common.result.Result;
import online.longzipeng.cinemasys.entity.OrderDetail;
import online.longzipeng.cinemasys.service.OrderDetailService;
import online.longzipeng.cinemasys.vo.order.OrderDetailSaveVO;
import online.longzipeng.cinemasys.vo.order.OrderDetailUpdateVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  订单详细控制器
 * </p>
 *
 * @author lzp
 * @since 2021-04-08
 */
@RestController
@RequestMapping("/order-detail")
@Api(value = "订单详情相关接口",description = "订单详情相关接口",tags = "订单详情相关接口")
public class OrderDetailController {

    @Autowired
    OrderDetailService orderDetailService;

    @PostMapping("/save")
    @NeedPermissions("orderDetail:save")
    @ApiOperation("新增订单详细")
    public Result save(@Valid @RequestBody OrderDetailSaveVO orderDetailSaveVO){
        OrderDetail orderDetail = new OrderDetail();
        BeanUtils.copyProperties(orderDetailSaveVO,orderDetail);
        orderDetailService.add(orderDetail);
        return Result.success(null,"订单详细添加成功！！");
    }

    @GetMapping("/delete-one")
    @NeedPermissions("orderDetail:delete")
    @ApiOperation("删除订单详细")
    public Result deleteOne(@ApiParam(value = "订单详细id",required = true)Long id){
        try {
            orderDetailService.removeById(id);
        } catch (Exception e) {
            e.printStackTrace();
            throw new AppException(ResponseEnum.FAILED);
        }
        return Result.success(null,"订单详细删除成功！！");
    }

    @GetMapping("/delete")
    @NeedPermissions("orderDetail:delete")
    @ApiOperation("删除多个订单详细")
    public Result deleteOne(@ApiParam(value = "订单详细id数组",required = true)Long[] ids){
        List<Long> idList = Arrays.stream(ids).collect(Collectors.toList());
        try {
            orderDetailService.removeByIds(idList);
        } catch (Exception e) {
            e.printStackTrace();
            throw new AppException(ResponseEnum.FAILED);
        }
        return Result.success(null,"订单详细删除成功！！");
    }

    @PostMapping("/update")
    @NeedPermissions("orderDetail:update")
    @ApiOperation("修改订单详细")
    public Result update(@Valid @RequestBody OrderDetailUpdateVO orderDetailUpdateVO) {
        OrderDetail orderDetail = new OrderDetail();
        BeanUtils.copyProperties(orderDetailUpdateVO,orderDetail);
        orderDetailService.updateOrderDetail(orderDetail);
        return Result.success(null,"订单详细修改成功！！");
    }
}
